package edu.moravian.world;

import edu.moravian.math.Point2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.awt.image.ImageObserver;
  
    /*
     * I'm thinking this should be some sort of adapter for graphics 2d and we
     * just implement the methods that we need.
     * 
     * It would be nice to pass in our points instead of an x and a y
     * and to have a method that draws an image centered on the points
     * 
     * Overall design should be that we pass the world 2d around and the only place we 
     * think about the graphics and coordinate translation would b ein the video controller
     * 
     * The basic things we need to do are to draw an image, rotate, dipose, draw string and maybe draw shapes
     * 
     * I still need to work on and test this and comment it but it seems to be working well
     * 
     * 
     * An idea for the text stuff is just to scale the font 
     * 
     * 
     * 
     * 
     * 
     * Methods to add:      Draw rotated Image
     *                      
     *                      Draw string in area 
     *                      Draw Centered multiline String in area (Fill in an area with text and break it up into whatever lines we have to)
     *                      Draw multiline string (strings with new lines)
     * 
     * Methods to fix:      Draw Strings - Base font size on world scale/still look into glyph vectors
     * 
     * 
     */


/**
 *
 * @author Elliot
 */
public class World2D 
{
    private CoordinateTranslator coordinateTranslator;
    private Graphics2D graphics2D;
    
  
    public World2D(Graphics2D graphics2D, CoordinateTranslator coordinateTranslator)
    {
        this.graphics2D = graphics2D;
        this.coordinateTranslator = coordinateTranslator;
    }
    
    public void setGraphics2D(Graphics2D graphics2D)
    {
        this.graphics2D = graphics2D;
    }
    
    
    
    public void drawCenteredRectangle(Point2D location, double width, double height)
    {
        
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(location);

        
        int screenWidth = coordinateTranslator.worldWidthScalerToScreenWidthScaler(width);
        int screenHeight = coordinateTranslator.worldHeightScalerToScreenHeightScaler(height);
        
        graphics2D.fillRect(screenPoint.x-(screenWidth/2), screenPoint.y-(screenHeight/2), screenWidth, screenHeight);
    }
    
    public void drawCenteredCircle(Point2D location, double radius)
    {
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(location);

        
        int screenWidth = coordinateTranslator.worldWidthScalerToScreenWidthScaler(radius) * 2;
        int screenHeight = coordinateTranslator.worldHeightScalerToScreenHeightScaler(radius) * 2;
        
        graphics2D.fillOval(screenPoint.x-(screenWidth/2), screenPoint.y-(screenHeight/2), screenWidth, screenHeight);
    }
    
    
    public void drawImage(Image image, Point2D bottomLeftPoint, double width, double height, ImageObserver observer)
    {
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(bottomLeftPoint);
        
        int screenWidth = coordinateTranslator.worldWidthScalerToScreenWidthScaler(width);
        int screenHeight = coordinateTranslator.worldHeightScalerToScreenHeightScaler(height);
        
        //we draw the image in terms of the translated x and y points. This method takes
        //the origion (bottom left corner) but graphics expects the origon to be the upper left
        //then we just pass in our appropriate height and width in terms of the screen size
        graphics2D.drawImage(image, screenPoint.x, screenPoint.y-screenHeight, screenWidth, screenHeight, observer);
        /*System.out.println("drawing image -  x: " + screenPoint.x + " y: " + (screenPoint.y-screenHeight) +
                " screenWidth: " + screenWidth + " screenHeight:" + screenHeight);*/
    }    
    
    
    
    
    //This is really convienent for drawing stuff centered at the specified point
    public void drawCenteredImage(Image image, Point2D centerPoint, double width, double height)
    {
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(centerPoint);
        
        int screenWidth = coordinateTranslator.worldWidthScalerToScreenWidthScaler(width);
        int screenHeight = coordinateTranslator.worldHeightScalerToScreenHeightScaler(height);
        
        graphics2D.drawImage(image, screenPoint.x-(screenWidth/2), screenPoint.y-(screenHeight/2), screenWidth, screenHeight, null);
        
    }
    
    //Is there a convienent way to draw scalled fonts?
    //draw the string starting from the top left of it 
    //this isn't right yet
    public void drawString(String string, Point2D upperLeftPoint)
    {
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(upperLeftPoint);
        
        Rectangle2D stringRect = graphics2D.getFontMetrics().getStringBounds(string, graphics2D);
            
       // int xOffSet = (int)(stringRect.getWidth()/2);
        int yOffSet = (int)(stringRect.getHeight());
        
        graphics2D.drawString(string, screenPoint.x, screenPoint.y + yOffSet);
    }
    
    //This centeer top of the string is the point passed in 
    public void drawTopCenteredString(String string, Point2D centerTopPoint)
    {
        Point screenPoint = coordinateTranslator.worldPointToScreenPoint(centerTopPoint);
        
        Rectangle2D stringRect = graphics2D.getFontMetrics().getStringBounds(string, graphics2D);
            
        int xOffSet = (int)(stringRect.getWidth()/2);
        int yOffSet = (int)(stringRect.getHeight());
        
        graphics2D.drawString(string, screenPoint.x - xOffSet, screenPoint.y + yOffSet);

        

        //glyph vectors?
       //Were going to need something more advanced for text
//        TextLayout tl = new TextLayout(string, graphics2D.getFont(), graphics2D.getFontRenderContext());
//        tl.draw(graphics2D, screenPoint.x - xOffSet, screenPoint.y + yOffSet);
                
        
    }
    
    public void setFont(Font font)
    {
        graphics2D.setFont(font);
    }
    
    public Font getFont()
    {
        return graphics2D.getFont();
    }
    
    public void setColor(Color color)
    {
        graphics2D.setColor(color);
    }
    
    public Color getColor()
    {
        return graphics2D.getColor();
    }
    
    
    public void dispose()
    {
        graphics2D.dispose();
    }
    
    
  
}
 